package com.example.controller;
import com.example.pojo.Result;
import com.example.pojo.UserManager;
import com.example.service.UserManagerService;
import com.example.service.impl.UserManagerServiceImp;
import com.example.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
public class LoginController {
   // private static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserManagerService userManagerService;

    @PostMapping("/login")
    public Result Login( @RequestBody  UserManager userManager){
        UserManager user = userManagerService.login(userManager);
        //如果登录成功，生成令牌并且返回令牌
        if(user!=null){
            Map<String,Object> claims = new HashMap<>();//设置要生成令牌的信息
            claims.put("id",user.getId());
            claims.put("name",user.getUsername());
            claims.put("password",user.getPassword());
           String jwt = JwtUtils.generateJwt(claims);//调用生成令牌的工具类，生成令牌
           return Result.success(jwt);//将令牌传给前端
        }
        //如果失败，就提示错误信息
        return Result.error("登录失败");
    }
  //  public Result Register (@RequestBody )
}
